package com.mdata.controller;

import com.mdata.common.ResultData;
import com.mdata.model.dto.wechat.dto.DepartmentCreateDTO;
import com.mdata.model.dto.wechat.dto.DepartmentUpdateDTO;
import com.mdata.model.dto.wechat.result.WeChatBaseResult;
import com.mdata.model.vo.DepartmentSimpleVO;
import com.mdata.model.vo.DepartmentTreeVO;
import com.mdata.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/department")
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询部门tree
     * @return
     */
    @GetMapping("/tree")
    public ResultData getDepartmentTree(){
        List<DepartmentTreeVO> departmentTree = departmentService.getDepartmentTree();
        return ResultData.success(departmentTree);
    }

    /**
     * 查询部门含人员的Tree
     * @return
     */
    @GetMapping("/simpleTree")
    public ResultData getDepartmentSimpleTree(){
        List<DepartmentSimpleVO> departmentTree = departmentService.getDepartmentSimpleTree();
        return ResultData.success(departmentTree);
    }

    /**
     * 新增部门
     * @param departmentCreateDTO
     * @return
     */
    @PostMapping("/create")
    public ResultData createDepartment(@RequestBody DepartmentCreateDTO departmentCreateDTO){
        Integer department = departmentService.createDepartment(departmentCreateDTO);
        return ResultData.success(department);
    }

    /**
     * 修改部门
     * @param departmentUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public ResultData updateDepartment(@RequestBody DepartmentUpdateDTO departmentUpdateDTO){
        WeChatBaseResult weChatBaseResult = departmentService.updateDepartment(departmentUpdateDTO);
        return ResultData.success(weChatBaseResult);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public ResultData deleteDepartment(Integer id){
        WeChatBaseResult weChatBaseResult = departmentService.deleteDepartment(id);
        return ResultData.success(weChatBaseResult);
    }
}
